package com.epoint.aop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 增、删、改操作的时候记录日志(spc_log)
 */
@Aspect
@Component
public class CheckUserSessionAop{


    //使用@Pointcut注解声明切入点表达式
    @Pointcut("execution(* com.epoint..*.*(..))")
    public void pt1(){}

    @Around("pt1()")
    public Object recordLog(ProceedingJoinPoint proceedingJoinPoint){
        Object proceed = null;

        // 类名（形如：com.epoint..........SpcinspectionAddaction）
        String className = proceedingJoinPoint.getSignature().getDeclaringTypeName();

        // 方法名（形如：getDataGridModel...）
        String methodName = proceedingJoinPoint.getSignature().getName();
        try {
            /**
             * 执行方法
             */
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

            if((className + "." + methodName).equals("com.epoint.rest.HtmlIndex.login") || requestAttributes == null){
                return proceedingJoinPoint.proceed();
            }

            HttpServletRequest request = requestAttributes.getRequest();
            HttpSession session = request.getSession();
            Object user = session.getAttribute("user");
            if(user != null){
                proceed = proceedingJoinPoint.proceed();
            }else{
                return "redirect:login.html";
            }
        }
        catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return proceed;
    }
}



